<!DOCTYPE html>
<html>
	<head>
		<title>我的消息</title>
		<meta charset="utf-8">
		<meta name="viewport"
			content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<!-- 所有的 css & js 资源 -->
		<link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
		<link rel="stylesheet" href="../../static/sa.css">

		<script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
		<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>

		<script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>

		<script src="https://unpkg.com/jquery@3.4.1/dist/jquery.min.js"></script>

		<script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>

		<script src="../../static/sa.js"></script>
		<script src="../../sa-resources/config.js"></script>

		<style type="text/css">
			.demo-table-expand {
				font-size: 0;
			}

			.demo-table-expand label {
				width: 90px;
				color: #99a9bf;
			}

			.demo-table-expand .el-form-item {
				margin-right: 0;
				margin-bottom: 0;
				width: 100%;
				border-bottom: 1px #eeeeee solid;
			}

			.el-table .nomodify-row {
				background: #e2e2e2;
			}
		</style>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<div class="c-panel">
				<!-- ------------- 参数栏 ------------- -->
				<div class="c-title">检索参数</div>
				<el-form>
					<div class="c-item">
						<label class="c-label">消息内容：</label>
						<el-input size="mini" v-model="queryParams[0].right[0]" placeholder="模糊查询"></el-input>
					</div>

					<div class="c-item" style="min-width: 0px;">
						<el-button size="mini" type="primary" icon="el-icon-search" @click="params.page = 1; f5()">查询
						</el-button>
					</div>
				</el-form>

				<!-- ------------- 快捷按钮 ------------- -->
				<div class="fast-btn">
					<el-button size="mini" type="success" icon="el-icon-view" @click="readAll()">全部已读</el-button>
					<el-button size="mini" type="info" icon="el-icon-refresh" @click="sa.f5()">重置</el-button>
				</div>

				<!-- ------------- 数据列表 ------------- -->
				<el-table :border="true" :data="dataList" size="small" ref="data-table" class="wuyan-table"
					row-key="id">
					<el-table-column align="center" type="selection" width="45px"></el-table-column>

					<el-table-column align="center" :label="msgType == 2 ? '审批人' : msgType == 3 ? '录入员' : '发送方'"
						prop="userId">
						<template slot-scope="s">
							<span>
								{{s.row.userId == 0 ? '系统' : s.row.userId == 1 ? '测试-管理员' : s.row.userId == 2 ? '测试-审批员' : '测试-录入员'}}
							</span>
						</template>
					</el-table-column>
					<el-table-column align="center" :label="msgType == 2 ? '录入员' : msgType == 3 ? '审批人' : '接收方'"
						prop="recvUserId">
						<template slot-scope="s">
							<span>
								{{s.row.recvUserId == 0 ? '系统' : s.row.recvUserId == 1 ? '测试-管理员' : s.row.recvUserId == 2 ? '测试-审批员' : '测试-录入员'}}
							</span>
						</template>
					</el-table-column>
					<el-table-column align="center" label="消息内容" prop="content"></el-table-column>

					<el-table-column align="center" label="阅读时间" prop="readTime">
						<template slot-scope="s">
							<span>{{timestampToTime(s.row.readTime)}}</span>
						</template>
					</el-table-column>

					<el-table-column align="center" :label="msgType == 2 ? '审批时间' : msgType == 3 ? '回复时间' : '创建时间'" prop="createTime">
						<template slot-scope="s">
							<span>{{timestampToTime(s.row.createTime)}}</span>
						</template>
					</el-table-column>

					<el-table-column align="center" label="操作" width="200px" fixed="right">
						<template slot-scope="s">
							<el-button v-if="s.row.targetId && msgType != 1" class="c-btn" type="success"
								icon="el-icon-caret-right" @click="show(s.row)">查看数据
							</el-button>

							<el-button v-if="!s.row.readTime" class="c-btn" type="primary" icon="el-icon-view"
								@click="read(s.row)">标记已读
							</el-button>
						</template>
					</el-table-column>

				</el-table>

				<!-- ------------- 分页 ------------- -->
				<div class="page-box">
					<el-pagination background layout="total, prev, pager, next, sizes, jumper"
						:current-page.sync="params.page" :page-size.sync="params.limit" :total="dataCount"
						:page-sizes="[15,30,50,100,500,1000]" @current-change="f5()" @size-change="f5()">
					</el-pagination>
				</div>
			</div>
		</div>

		<script src="../../sa-resources/api.js"></script>
		<script src="../../sa-resources/wuyan.js"></script>
		<script src="../../sa-resources/element-ui-helper.js"></script>
		<script>
			var app = new Vue({
				el: '.vue-box',
				data: {
					// 消息类型
					msgType: parseInt(sa.p('type', 0)),
					// 请求参数
					params: {
						isPage: true,
						page: 1,
						limit: 15,
						orders: '[]',
						params: '[]',
					},
					// 查询参数
					queryParams: [{
							left: 'content',
							op: 'like',
							right: ['']
						},
					],
					// 排序参数
					orderParams: [],
					// 当前数据
					dataList: [],
					// 数据量
					dataCount: 0,
					// 当前选择条目-单项
					chooseData: {
						row: null,
						field: null,
					},
				},
				created: function() {
					axiosConfig(axios)
					sa.onInputEnter(); // 监听输入框的回车事件，执行查询  
				},
				mounted() {
					this.f5();
				},
				methods: {
					// 刷新
					f5: function() {
						let loading = elLoadding({
							that: this
						})

						if (this.msgType) {
							this.queryParams.push({
								left: 'type',
								op: 'eq',
								right: [this.msgType]
							})
						}
						this.queryParams.push({
							left: 'recvUserId',
							op: 'eq',
							right: [user.info.id]
						})
						

						this.params.params = queryParamsToJsonStr(this.queryParams)
						this.params.orders = JSON.stringify(this.orderParams)
						axios.get('/wuyan/api/pub-msg', {
								params: this.params
							})
							.then(res => {
								this.dataList = res.list ? res.list : []; // 数据
								this.dataCount = res.count; // 分页

								sa.f5TableHeight(); // 刷新表格高度

								loading.close();
							})
							.catch((err) => { // 请求失败处理
								this.$notify.info({
									title: '消息',
									message: '初始化数据失败'
								});

								loading.close();
							});
					},

					// 查看数据
					show(data) {
						this.read(data)
						
						let ids = data.targetId.split(':')
						if(ids.length < 2){
							sa.error('信息有误，不可跳转')
							return
						}
						
						if (this.msgType == 1) {

						} else if (this.msgType == 2) {
							sa_admin.showTab({
								id: 'data-id-' + ids[1].replace('.', '-'),
								name: '查看数据(' + data.targetId + ')',
								url: 'page/data-input/list.html?id='+ ids[0] +'&dataId=' + ids[1]
							});
						} else {
							sa_admin.showTab({
								id: 'data-id-' + ids[1].replace('.', '-'),
								name: '查看数据(' + data.targetId + ')',
								url: 'page/data-approval/list.html?id='+ ids[0] +'&dataId=' + ids[1]
							});
						}
					},

					// 标记已读
					read: function(data) {
						let loading = elLoadding({
							that: this
						})

						let url = getUrl('pub-msg', 'read', [data.id])
						let http = getHttp(axios, 'pub-msg', 'read')
						http(url, {}).then(res => {
							parent.refulshMsg(true)

							this.f5();
							sa.onInputEnter(); // 监听输入框的回车事件，执行查询  
							loading.close();
						}).catch((err) => { // 请求失败处理
							sa.error(err.msg);
							loading.close();
						});
					},

					// 全部已读
					readAll: function() {
						// 提交删除 
						let loading = elLoadding({
							that: this
						})

						let url = getUrl('pub-msg', 'read-all')
						let http = getHttp(axios, 'pub-msg', 'read-all')
						http(url, {}).then(res => {
							sa.ok('标记成功')
							
							parent.refulshMsg(true)

							this.f5();
							sa.onInputEnter(); // 监听输入框的回车事件，执行查询  
							loading.close();
						}).catch((err) => { // 请求失败处理
							sa.error(err.msg);
							loading.close();
						});
					},

					// 表格样式控制
					tableRowClassName({
						row,
						rowIndex
					}) {
						if (row.cfgModify !== 1) {
							return 'nomodify-row';
						}

						return '';
					}
				}
			})
		</script>
	</body>
</html>
